#include "TraceManager.h"
#include "ScopedLock.h"
#include "MessageLogEntry.h"
#include <sstream>
#include <iomanip>

TraceManager* TraceManager::traceManager;
//Lock TraceManager::messageVectorLock;

TraceManager::TraceManager(void)
{
	traceLog = new std::vector<MessageLogEntry*>();
	displayableMessageSenders = new std::vector<std::string>();

	startTime = time(NULL);
}


TraceManager::~TraceManager(void)
{

}

TraceManager* TraceManager::SharedManager()
{
	if (traceManager == NULL)
	{
		traceManager = new TraceManager();
	}

	return traceManager;
}

void TraceManager::addMessage(std::string message, time_t currentTime, Agent* sender)
{
	{
		ScopedLock scopedMessageVectorLock (&messageVectorLock);

		std::string agentName;
		std::string outputString;
		bool isBroken;
		//bool display = false;

		agentName = sender->getName();
		isBroken = sender->isBroken;

		//int colonIndex = message.find_first_of(':', 0) + 1;
		//agentName = message.substr(0, colonIndex);	//includes the :
		//std::string messageContent = message.substr(colonIndex);
		 

		std::stringstream ss;

		if(isBroken)
		{
			ss << "<p style=\"color:red;\">";
		}

		ss << "[" << currentTime - startTime << "] " << message;

		if(isBroken)
		{
			ss << "</p>";
		}
		getline(ss, outputString);

		/*
		for(int i = 0; i < this->displayableMessageSenders->size(); i++)
		{
			if(this->displayableMessageSenders->at(i) == agentName)
			{
				display = true;
				break;
			}
		}
		*/

		//traceLog->push_back(new MessageLogEntry(sender, currentTime, sender->getName(), outputString, display));
		traceLog->push_back(new MessageLogEntry(sender, currentTime, sender->getName(), outputString, sender->messagesWillBeDisplayed));
	}
}
